<template>
  <div>
    <t-menu :value="active">
      <img class='logo' src='../../assets/icon.png' alt='logo' />
      <template v-for="item in list" :key="item.path">
        <t-menu-item v-if="getHref(item)" :name="item.path" :value="getPath(item)" onclick="openHref(getHref(item)[0])">
          <template #icon>
            <component :is="menuIcon(item)"></component>
          </template>
          {{ item.title }}
        </t-menu-item>
      </template>
    </t-menu>
  </div>
</template>
<script setup lang="ts">
import type { PropType } from 'vue'
import { computed } from 'vue';
import { useRoute } from 'vue-router';
import type { MenuRoute } from '../../types/interface.d.ts';

type ListItemType = MenuRoute & { icon?: string };

const props = defineProps({
  navData: {
    type: Array as PropType<MenuRoute[]>,
    default: () => [],
  },
  theme: {
    type: String as PropType<'dark' | 'light'>,
    default: 'light',
  },
});
const route = useRoute();


const getActive = (maxlevel = 3): string => {
  if (!route.path) {
    return '';
  }
  return route.path.split('/')
    .filter((_item: string, index: number) => index <= maxlevel && index > 0)
    .map((item: string) => `/${item}`)
    .join('');
}

const getMenuList = (list: MenuRoute[], basePath?: string): ListItemType[] => {
  if (!list || list.length === 0) {
    return [];
  }
  list.sort((a, b) => {
    return (a.meta?.orderNo || 0) - (b.meta?.orderNo || 0);
  });

  return list
    .map((item) => {
      const path = basePath;
      return {
        path,
        title: item.meta?.title,
        icon: item.meta?.icon,
        children: getMenuList(item.children, path),
        meta: item.meta,
        redirect: item.redirect,
      };
    })
    .filter((item) => item.meta && item.meta.hidden !== true);
};

const active = computed(() => getActive());
const list = computed(() => {
  const { navData } = props;
  return getMenuList(navData);
})

const getHref = (item: MenuRoute) => {
  const { frameSrc, frameBlank } = item.meta;

}
</script>
<style lang="less" scoped>
.logo {
  width: var(--td-size-12);
  height: var(--td-size-12);
  margin: var(--td-comp-paddingTB-s) 0 var(--td-comp-paddingTB-l) 0;
}
</style>
